@extends('adminDialog')

@section('content')
    <form action="{{url('admin/ads/update/'.$model->id)}}" method="POST" class="J_ajaxForm">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="pop_cont pop_table">
            <table width="100%">
                <tbody>
                <tr>
                    <th><label for="posi_id">广告位 <span class="red">*</span></label></th>
                    <td>
                        <select name="posi_id" id="posi_id">
                            @foreach($posi as $item)
                            <option value="{{$item->id}}" @if ($item->id == $model->posi_id) selected="selected" @endif>{{ $item->name }} [{{$item->width}}px * {{$item->height}}px]</option>
                            @endforeach
                        </select>
                    </td>
                </tr>

                <tr>
                    <th><label for="start">开始时间 <span class="red">*</span></label></th>
                    <td><input type="text" name="start" id="start" value="{{$model->start}}" class="input length_2 J_datetime"/></td>
                </tr>

                <tr>
                    <th><label for="end">开始时间 <span class="red">*</span></label></th>
                    <td><input type="text" name="end" id="end" value="{{$model->end}}" class="input length_2 J_datetime"/></td>
                </tr>

                <tr>
                    <th><label for="pic">图片 <span class="red">*</span></label></th>
                    <td>
                        <input type="hidden" name="pic" id="pic" value="{{$model->pic}}"/>
                        <span id="upload_btn" class="cc"><img src="{{url('/img/ads-'.intval($model->pic).'/100-100')}}" /></span>
                        <div class="cc gray">建议上传和广告位的尺寸相符的图片</div>
                    </td>
                </tr>

                <tr>
                    <th><label for="mode">模型 <span class="red">*</span></label></th>
                    <td>
                        <select name="mode" id="mode">
                        @foreach($modes as $k => $item)
                            <option value="{{ $k }}" @if ($k == $model->mode) selected="selected" @endif rel="{{ $item[key($item)] }}">{{key($item)}}</option>
                        @endforeach
                        </select>
                    </td>
                </tr>
                
                <tr>
                    <th><label for="url">链接</label></th>
                    <td>
                        <input type="url" name="url" id="url" value="{{$model->url}}" class="input length_5" />
                        <div class="gray cc">可以自行补全参数，如 ?storeid=5 </div>
                    </td>
                </tr>
                
                <tr>
                    <th><label for="sort">排序</label></th>
                    <td>
                        <input type="number" name="sort" id="sort" class="input length_1" value="{{ $model->sort }}"/>
                        <div class="cc gray">数值越大越靠前</div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <div class="pop_bottom cc">
            <button type="reset" class="btn fr" id="J_dialog_close">取消</button>
            <button type="submit" class="btn btn_submit J_ajax_submit_btn fr mr10" id="J_dialog_close">提交</button>
        </div>
    </form>
@stop

@section('style')
    <style>
        .body_none {width: 500px;}
        #upload_btn {cursor:pointer; display: inline-block;}
        #upload_btn img {border:1px solid #ddd; padding:2px;}
    </style>
@stop

@section('js')
    @parent
    <script src="{{asset('/js/admin/admin.js')}}"></script>
    <script src="{{asset('/js/plupload/plupload.full.min.js')}}"></script>
    <script>
        var uploader = new plupload.Uploader({
            browse_button : 'upload_btn',
            url : '{{url('/upimg')}}',
            flash_swf_url : '{{asset('/js/plupload/Moxie.swf')}}',
            silverlight_xap_url: '{{asset('/js/plupload/Moxie.xap')}}',
            filters : {
                mime_types : [
                    {title: '图片文件', extensions: "jpg,gif,png"}
                ],
                max_file_size: '10mb'
            },
            multi_selection : false,
            file_data_name  : 'file',
            multipart_params: {'_token': GV.TOKEN, 'dir': 'ads'}
        });
        uploader.init();

        uploader.bind('Error', function (uploader, errObject) {
            Wind.dialog.alert(errObject.message);
        });

        uploader.bind('FilesAdded', function (uploader, files) {
            uploader.start();
        });
        uploader.bind('FileUploaded', function (uploader, file, responseObj) {
            var ret = $.parseJSON(responseObj.response);
            if (! ret.id)
                Wind.dialog.alert(ret.message);
            else {
                $('#pic').val(ret.id);
                $('#upload_btn').find('img').attr('src', '{!! url("img/ads-' + ret.id + '/100-100") !!}');
            }
        });

        $('#mode').on('change',function(){
            var base = $(this).find('option:selected').attr('rel');
            $('#url').val(base);
        });

    </script>
@stop